package com.iworkh.spring.web.mvc.controller;

import com.iworkh.spring.web.mvc.dao.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Reqeust handler Controller
 *
 * @author: iworkh-沐雨云楼
 * @date: 2019-12-25
 */
@Controller
@RequestMapping("/springmvc")
public class RequestHandlerController {

    @RequestMapping("/testRequestMappingValue")
    public String testRequestMappingValue() {
        System.out.println("iworkh-testRequestMappingValue");
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestMappingMethodGet", method = RequestMethod.GET)
    public String testRequestMappingMethodGet() {
        System.out.println("iworkh-testRequestMappingMethodGet");
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestMappingMethodPost", method = RequestMethod.POST)
    public String testRequestMappingMethodPost() {
        System.out.println("iworkh-testRequestMappingMethodPost");
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestMappingParams", params = {"userName", "age!=28"})
    public String testRequestMappingParams() {
        System.out.println("iworkh-testRequestMappingParams");
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestMappingHeaders", headers = {"Accept-Language=zh-CN,zh;q=0.9"})
    public String testRequestMappingHeaders() {
        System.out.println("iworkh-testRequestMappingHeaders");
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestMappingAntPattern/*/a")
    public String testRequestMappingAntPattern() {
        System.out.println("iworkh-testRequestMappingAntPattern");
        return "iworkh";
    }


    @RequestMapping(value = "/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") String id) {
        System.out.println("iworkh-testPathVariable: " + id);
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestParam")
    public String testRequestParam(@RequestParam("userName") String userName, @RequestParam(value = "age",
                                                                                            required = false,
                                                                                            defaultValue = "1") int age) {
        System.out.println("iworkh-testPathVariable: " + userName + "-" + age);
        return "iworkh";
    }

    @RequestMapping(value = "/testRequestHeader")
    public String testRequestHeader(@RequestHeader("Host") String host, @RequestHeader(value = "Accept-Encoding",
                                                                                       required = false,
                                                                                       defaultValue = "UTF-8") String encoding) {
        System.out.println("iworkh-testRequestHeader: " + host + "-" + encoding);
        return "iworkh";
    }

    @RequestMapping(value = "/testCookieValue")
    public String testCookieValue(@CookieValue("JSESSIONID") String jessionId) {
        System.out.println("iworkh-testCookieValue: " + jessionId);
        return "iworkh";
    }


    @RequestMapping(value = "/testPojoParam")
    public String testPojoParam(Student student) {
        System.out.println("iworkh-testPojoParam: " + student);
        return "iworkh";
    }

    @RequestMapping(value = "/testServletParam")
    public String testServletParam(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("iworkh-testServletParam-request: " + request);
        System.out.println("iworkh-testServletParam-response: " + response);
        return "iworkh";
    }
}
